package com.panda.progress.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.panda.group.entity.GroupMemberAcce;
import com.panda.group.service.GroupService;
import com.panda.progress.entity.MyCourses;
import com.panda.progress.entity.Progress;
import com.panda.progress.service.ProgressService;
import com.panda.sys.entity.User;
import com.panda.utils.CommonUtils;
import com.panda.utils.contantUtil;

@Controller
public class ProgressController {
	@Resource
	private ProgressService progressService;
	@Resource
	private GroupService groupService;
	
	@RequestMapping("/prog/myProg")
	public String myProg(Model model,Long gid, HttpServletRequest request){
		User user = (User) request.getSession().getAttribute(contantUtil.SESSION_USER);
		MyCourses myCourses=progressService.findMyCourses(user.getId(),gid);
		List<Progress> progress=progressService.findByUId(user.getId(),gid);
		List<GroupMemberAcce> progressAcces=groupService.findMemberAcce(user.getId(),gid);
		model.addAttribute("myCourses", myCourses);
		model.addAttribute("progress", progress);
		model.addAttribute("progressAcces", progressAcces);
		return "progress/myProgress";
	}
	
	@ResponseBody
	@RequestMapping("/prog/save")
	public String save(Progress progress, HttpServletRequest request){
		User user = (User) request.getSession().getAttribute(contantUtil.SESSION_USER);
		MyCourses myCourses=progressService.findMyCourses(user.getId(),progress.getGid());
		progress.setUid(user.getId());
		progress.setGid(myCourses.getGroupId());
		Long id=progressService.saveOrUpdate(progress);
		return CommonUtils.msgCallback(true,"保存成功",null, id.toString());
	}
	
	@ResponseBody
	@RequestMapping("/prog/saveComment")
	public String saveComment(Progress progress){
		Long id=progressService.saveComment(progress);
		return CommonUtils.msgCallback(true,"保存成功",null, id.toString());
	}
	
	@ResponseBody
	@RequestMapping("/prog/delete")
	public String delete(Long id){
		progressService.deleteById(id);
		return CommonUtils.msgCallback(true,"删除成功",null, null);
	}
	
	@ResponseBody
	@RequestMapping("/prog/saveAcce")
	public String saveAcce(GroupMemberAcce acce, HttpServletRequest request){
		groupService.saveAcce(acce);
		return CommonUtils.msgCallback(true,"保存成功",null, null);
	}
	
	@ResponseBody
	@RequestMapping("/prog/deleteAcce")
	public String deleteAcce(Long id){
		groupService.deleteAcce(id);
		return CommonUtils.msgCallback(true,"删除成功",null, null);
	}
}
